package com.yungu.swift.demo.mq.consumer;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.yungu.swift.demo.mq.model.LogDto;
import com.yungu.swift.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

/**
 * @author Zerrion
 */
@Component
@Slf4j
public class LogMqListener {
    @Autowired
    private ObjectMapper objectMapper;

    /**
     * 监听消费用户日志
     *
     * @param message
     */
    @RabbitListener(queues = "${log.queue.name}", containerFactory = "singleListenerContainer")
    public void consumeLogQueue(@Payload byte[] message) {
        try {
            LogDto logDto = objectMapper.readValue(message, LogDto.class);
            log.info("监听消费用户日志 监听到消息： {} ", logDto);
            //TODO：记录日志入数据表
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @RabbitListener(queues = "loginLogQueue")
    public void consumeLoginLogQueue(LogDto logDto) {
        log.info("监听消费用户日志 监听到消息： {} ", JsonUtils.toJSONString(logDto));
    }
}
